package com.shengqi.xinnongren.work.ui.goods.fragments;


import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ProgressBar;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpFragment;
import com.shengqi.xinnongren.work.ui.goods.adapter.CommentsAdapter;
import com.shengqi.xinnongren.work.ui.goods.model.CommentBean;
import com.shengqi.xinnongren.work.ui.goods.presenter.BrowseCommentPresenter;
import com.shengqi.xinnongren.work.ui.goods.view.BrowseCommentView;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

/**
 * A simple {@link Fragment} subclass.
 */
public class BrowseCommentFragment extends BaseMvpFragment<BrowseCommentView, BrowseCommentPresenter> implements BrowseCommentView {

    //1为有图片,其他随意
    public static final int TYPE_PIC = 1;
    public static final int TYPE_ALL = 2;
    private static final String EXTRA_GOODS_ID = "goods_id";
    private static final String EXTRA_TYPE = "type";

    @BindView(R.id.rv_comments)
    RecyclerView mRecyclerView;

    @BindView(R.id.pb_loading)
    ProgressBar mProgressBar;

    String mGoodsId;
    int mType = TYPE_ALL;
    private int mCurrentPage;

    @BindView(R.id.sr_refresh)
    SmartRefreshLayout mRefreshLayout;
    private ArrayList<CommentBean> mCommentBeanList;
    private CommentsAdapter mAdapter;


    public BrowseCommentFragment() {
    }

    public static BrowseCommentFragment newInstance(String goodsId, int type) {
        BrowseCommentFragment fragment = new BrowseCommentFragment();
        Bundle args = new Bundle();
        args.putString(EXTRA_GOODS_ID, goodsId);
        args.putInt(EXTRA_TYPE, type);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected int setLayoutResource() {
        return R.layout.fragment_browse_comment;
    }

    @Override
    protected void lazyLoadData() {
        getData(false);
    }

    @Override
    protected void initView() {
        mRefreshLayout.setOnRefreshListener(refreshLayout -> getData(false));
        mRefreshLayout.setOnLoadMoreListener(refreshLayout -> getData(true));
        mCommentBeanList = new ArrayList<>();
        mAdapter = new CommentsAdapter(getContext(), mCommentBeanList);
        mRecyclerView.setAdapter(mAdapter);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
    }

    private void getData(boolean isLoadMore) {
        if (isLoadMore)
            mCurrentPage++;
        else
            mCurrentPage = 0;
        getPresenter().getGoodsDetails(mGoodsId, mType, mCurrentPage);
    }


    @Override
    protected void initData() {
        mGoodsId = getArguments().getString(EXTRA_GOODS_ID);
        mType = getArguments().getInt(EXTRA_TYPE);
        if (mType != TYPE_ALL && mType != TYPE_PIC)
            throw new RuntimeException("老铁，类型不能瞎传");
    }

    @Override
    protected BrowseCommentPresenter createPresenter() {
        return new BrowseCommentPresenter();
    }

    @Override
    public void onGetCommentListSucceed(List<CommentBean> commentBeanList) {
        resetRefreshLayout();
        if (mCurrentPage == 0) {
            mCommentBeanList.clear();
        }
        mCommentBeanList.addAll(commentBeanList);
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void showLoadingView() {

    }

    @Override
    public void hideLoadingView() {
        resetRefreshLayout();
    }

    @Override
    public void onNetworkError() {
        resetRefreshLayout();
    }

    private void resetRefreshLayout() {
        mProgressBar.setVisibility(View.GONE);
        mRefreshLayout.finishLoadMore();
        mRefreshLayout.finishRefresh();
    }
}
